<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">所属农场</label>
        <el-select v-model="query.farm_id" filterable placeholder="请选择" style="width: 185px;" class="filter-item">
          <el-option v-for="item in dict.farms" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <label class="el-form-item-label">田块名称</label>
        <el-input
          v-model="query.field_name"
          clearable
          placeholder="田块名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">收割类型</label>
        <el-select
          v-model="query.shouge_type"
          filterable
          placeholder="请选择"
          style="width: 185px;"
          class="filter-item"
        >
          <el-option v-for="item in dict.shouge_type" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="600px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="所属农场" prop="farm_id">
            <el-select
              v-model="form.farm_id"
              filterable
              placeholder="请选择"
              style="width: 370px;"
              @change="changeFram"
            >
              <el-option v-for="item in dict.farms" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="田块" prop="field_id">
            <el-select v-model="form.field_id" filterable placeholder="请选择" style="width: 370px;">
              <el-option v-for="item in fields" :key="item.id" :label="item.field_name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="收割类型" prop="shouge_type">
            <el-select v-model="form.shouge_type" filterable placeholder="请选择" style="width: 185px;">
              <el-option v-for="item in dict.shouge_type" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>

          <el-form-item label="作业时间">
            <el-date-picker
              v-model="form.shouge_time"
              type="datetime"
              style="width: 370px;"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item label="收割作物">
            <el-input v-model="form.shouge_zuowu" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="收割设备">
            <el-input v-model="form.shouge_shebei" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="收割参数">
            <el-input v-model="form.shouge_canshu" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="作业轨迹">
            <el-input v-model="form.tack" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="farm_name" label="所属农场" width="200" />
        <el-table-column prop="field_name" label="田块名称" width="200" />
        <el-table-column prop="shouge_type" label="收割类型" width="200" />
        <el-table-column prop="shouge_time" label="作业时间" width="200" />
        <el-table-column prop="shouge_zuowu" label="收割作物" width="200" />
        <el-table-column prop="shouge_shebei" label="收割设备" width="200" />
        <el-table-column prop="shouge_canshu" label="收割参数" width="200" />
        <el-table-column prop="tack" label="作业轨迹" width="200" />
        <el-table-column prop="remark" label="备注" width="200" />
        <el-table-column
          v-if="checkPer(['admin', 'fieldShouge:edit', 'fieldShouge:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
/** 收割履历 */
import crudFieldShouge from '@/api/jf/fieldShouge'
import { getFieldListByFarm } from '@/api/jf/public'
import CRUD, { crud, form, header, presenter } from '@crud/V2/crud'
import crudOperation from '@crud/V2/CRUD.operation'
import pagination from '@crud/V2/Pagination'
import rrOperation from '@crud/V2/RR.operation'
import udOperation from '@crud/V2/UD.operation'

const defaultForm = {
  id: null,
  field_id: null,
  farm_id: null,
  shouge_type: null,
  shouge_time: null,
  shouge_zuowu: null,
  shouge_shebei: null,
  shouge_canshu: null,
  tack: null,
  remark: null
}
export default {
  name: 'FieldShouge',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '田块收割履历',
      url: 'api/v2/base/farm/fieldShouge',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudFieldShouge }
    })
  },
  dictsV2: ['farms'],
  dicts: ['shouge_type'],
  data() {
    return {
      permission: {
        add: ['admin', 'fieldShouge:add'],
        edit: ['admin', 'fieldShouge:edit'],
        del: ['admin', 'fieldShouge:del']
      },
      rules: {
        field_id: [{ required: true, message: '不能为空', trigger: 'blur' }],
        farm_id: [{ required: true, message: '不能为空', trigger: 'blur' }],
        shouge_type: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      queryTypeOptions: [
        { key: 'farm_id', display_name: '农场编号' },
        { key: 'field_name', display_name: '田块名称' },
        { key: 'shouge_type', display_name: '收割类型' }
      ],
      fields: []
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.getFields(form.farm_id, true)
    },
    // 获取田块
    getFields(farm_id, isNotClear) {
      if (farm_id != null) {
        getFieldListByFarm(farm_id)
          .then(res => {
            this.fields = res.data
          })
          .catch(() => {})
      } else {
        this.fields = []
      }
      if (!isNotClear) {
        this.form.field_id = ''
      }
    },
    changeFram(value) {
      this.getFields(value)
    }
  }
}
</script>

<style scoped></style>
